﻿#include "SetupController.h"
#include "IndexWindow.h"

SetupController::SetupController(){}

SetupController::~SetupController(){}

void SetupController::viewDidLoad(){
	CAView* view=this->getView();
	this->setNavigationBarItem(CANavigationBarItem::create("系统设置"));
	
	CAView* subview=CAView::createWithLayout(DLayoutFill);
	subview->enabledBottomShadow(true);
	table=CATableView::createWithLayout(DLayoutFill);
	table->setAllowsSelection(true);
	table->setAllowsMultipleSelection(false);
	table->setShowsScrollIndicators(false);
	subview->addSubview(table);
	table->onNumberOfRowsAtIndexPath([&](unsigned int section){
		return datas.size();
	});
	table->onCellHeightAtIndexPath([](unsigned int section, unsigned int row){
		return 100;
	});
	table->onCellAtIndexPath([&](DSize cellSize, unsigned int section, unsigned int row){
		CATableViewCell* cell=dynamic_cast<CATableViewCell*>(table->dequeueReusableCellWithIdentifier("setup_cell"));
		if(cell==nullptr){
			cell=CATableViewCell::create("setup_cell");
			CAView* subview=cell->getContentView();
			CALabel* name=CALabel::createWithLayout(DLayout(DHorizontalLayout_L_W(10, 300), DVerticalLayout_H_C(35, 0.5)));
			name->setTag(100);
			name->setFontSize(35);
			name->setColor(ccc4(51, 51, 51, 255));
			name->setTextAlignment(CATextAlignment::Left);
			name->setVerticalTextAlignmet(CAVerticalTextAlignment::Center);
			subview->addSubview(name);

			CAImageView* advance=CAImageView::createWithLayout(DLayout(DHorizontalLayout_R_W(20, 16), DVerticalLayout_H_C(28, 0.5)));
			advance->setTag(200);
			advance->setImage(CAImage::create("img/advance.png"));
			subview->addSubview(advance);
		}
		cell->setDraggingLength(0);

		CSJson::Value val=datas[row];
		CAView* subview=cell->getContentView();
		CALabel* name=(CALabel*)subview->getSubviewByTag(100);
		name->setText(val["name"].asCString());

		return cell;
	});
	view->addSubview(subview);
	subview=CAView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_H(430, 70)));
	subview->enabledTopShadow(true);
	subview->enabledBottomShadow(true);
	CAButton* btn=CAButton::createWithLayout(DLayoutFill, CAButton::Type::Custom);
	btn->setTitleFontSize(40);
	btn->setTitleForState(CAControl::State::Normal, "退出登录");
	btn->setTitleColorForState(CAControl::State::Normal, ccc4(51, 51, 51, 255));
	btn->addTarget([]{
		CAApplication* application=CAApplication::getApplication();
		application->purgeCachedData();
		application->runWindow(IndexWindow::getInstance());
		RootWindow::destroyInstance();
	}, CAButton::Event::TouchDown);
	subview->addSubview(btn);
	view->addSubview(subview);

	this->fillData();
}

void SetupController::fillData(){
	CSJson::Value obj1;
	obj1["name"]="设置支付密码";
	datas.append(obj1);
	CSJson::Value obj2;
	obj2["name"]="修改登录密码";
	datas.append(obj2);
	CSJson::Value obj3;
	obj3["name"]="更换手机号";
	datas.append(obj3);
	
	table->reloadData();
}